package com.clkj.mobileinspector.ui.activity;

import android.os.Bundle;

import com.clkj.mobileinspector.R;
import com.clkj.mobileinspector.model.Constant;
import com.clkj.mobileinspector.model.ObserverOnNext;
import com.clkj.mobileinspector.model.OkHttpUtils;
import com.clkj.mobileinspector.model.bean.QueryBean;
import com.clkj.mobileinspector.model.bean.QuerySuccessBean;
import com.clkj.mobileinspector.model.bean.ResourceApi;
import com.clkj.mobileinspector.ui.adapter.FragmentAdapter;
import com.clkj.mobileinspector.ui.fragment.ListFragment;
import com.clkj.mobileinspector.ui.fragment.MonthlyAnalysisFragment;
import com.clkj.mobileinspector.ui.fragment.ProblemFragment;
import com.clkj.mobileinspector.ui.fragment.TypeContrastFragment;
import com.clkj.mobileinspector.utils.JsonUtils;
import com.clkj.mobileinspector.utils.ResourceApiUtils;
import com.google.android.material.tabs.TabLayout;
import com.lws.basic.base.BaseActivity;
import com.lws.basic.base.BaseFragment;
import com.lws.basic.utils.common.MyToast;
import com.lws.basic.view.adapter.CustomViewPager;

import java.util.ArrayList;
import java.util.List;

import androidx.fragment.app.FragmentPagerAdapter;
import androidx.viewpager.widget.ViewPager;
import butterknife.BindView;
import butterknife.ButterKnife;

//汇总分析
public class StatisticsActivity extends BaseActivity {


    @BindView(R.id.tl)
    TabLayout mTl;
    @BindView(R.id.vp)
    CustomViewPager mVp;

    public  List<QuerySuccessBean.DataBean.DataListBean.FieldValuesBean> getSpList() {
        return spList;
    }

    private List<QuerySuccessBean.DataBean.DataListBean.FieldValuesBean> spList ;

    @Override
    public int setLayoutId() {
        return R.layout.activity_statistics;
    }



    @Override
    public void initData() {
        super.initData();
        ButterKnife.bind(this);
       // initViewPager();
        getData(null);
    }

    private void initViewPager(){
        List<String> title = new ArrayList<>();
        title.add("列表");
        title.add("类型对比");
        title.add("月度分析");
        title.add("问题占比");
        List<BaseFragment> fragmentList = new ArrayList<>();
        fragmentList.add(new ListFragment());
        fragmentList.add(new TypeContrastFragment());
        fragmentList.add(new MonthlyAnalysisFragment());
        fragmentList.add(new ProblemFragment());
        mVp.setAdapter(new FragmentAdapter(getSupportFragmentManager(), fragmentList, title));
        mVp.setScroll(false);
        mTl.setupWithViewPager(mVp);
    }

    /**
     * 获取数据下拉列表
     */
    private void getData(List<QueryBean.ParameterBean.ConditionBean.KeyValueListBean> keyValueListBeanList) {
        ResourceApi policeDept = ResourceApiUtils.getResourceApi(MainActivity.resourceApiList, Constant.policeDept);

        showLoading();
        List<QueryBean.ParameterBean.OrderByBean> orderByList = new ArrayList<>();


        QueryBean.ParameterBean.PageBean page = new QueryBean.ParameterBean.PageBean();
        page.setPageNo(1);
        page.setPageSize(1000);

        String json = JsonUtils.query(keyValueListBeanList, "policedept,problemtype"
                , policeDept.getResourceId(), orderByList, page, policeDept.getResourceRegionalismCode());

        OkHttpUtils.post(policeDept.getResourceAddress(), json, QuerySuccessBean.class).compose(this.bindToLifecycle()).subscribe(new ObserverOnNext<QuerySuccessBean>() {
            @Override
            public void onNext(QuerySuccessBean querySuccessBean) {
                dismissLoading();

                if (querySuccessBean.getCode().equals("200")) {

                    spList = querySuccessBean.getData().getDataList().get(0).getFieldValues();
                    initViewPager();

                } else {
                    MyToast.show(querySuccessBean.getMessage());
                }
            }
        });
    }


}
